/**
 * 摇杆类
 * Author：  suyuyi
 * Date：    2017-4-15
 */
cc.Class({
    extends: cc.Component,

    properties: {
        
    },

     // use this for initialization
    onLoad: function () {
        this.touchValid = true;
        this.touchAreaRedius = 100;
        this.touchStop = 5;
        this.distanceToCircle = 0;
        
        //this.plane = cc.find("plane_hero",this.node.parent).getComponent("plane1");
       
       //cc.find("background/plane_hero",this.node.parent).getComponent("plane1");
       // cc.log(this.plane)
        this.stick  = cc.find("stick",this.node);
       // this.stick.setPosition(-400, -250);
        
        this.stick.opacity = 255;
        this.circleCenter = this.stick.getPosition();
       //cc.log(this.circleCenter);
        // this._listener = { 
        //     event           : cc.EventListener.TOUCH_ONE_BY_ONE,
        //     swallowTouches  : false,  
        //     onTouchBegan    : this._onTouchBegan.bind(this),
        //     onTouchMoved    : this._onTouchMoved.bind(this),
        //     onTouchEnded    : this._onTouchEnded.bind(this),
        //     onTouchCancelled: this._onTouchEnded.bind(this)
        // }
        // this.bg = cc.find("background",this.node.parent);
        // cc.eventManager.addListener(this._listener, this.bg);
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchBegin, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    },

    onTouchBegin: function(event) {
       // cc.log("ok");
        
        var localPos = this.node.convertTouchToNodeSpace(event);
       // localPos.x-=100;
        //localPos.y-=100;
        //cc.log(localPos);
        localPos = cc.v2(localPos.x - this.node.width / 2, localPos.y - this.node.height / 2);

        if (localPos.mag() > this.touchAreaRedius){
            this.touchValid = false;
        }
        else {
            this.touchValid = true;
            this.stick.opacity = 255;
            //this.updateStick(localPos);
        }
        
        event.stopPropagation();
    },

    onTouchMove: function(event) {
       //cc.log("move");
        var localPos = this.node.convertTouchToNodeSpace(event); //convertToWorldSpace
        //localPos.x-=100;
       // localPos.y-=100;
        //cc.log(localPos)
        //cc.log(this.circleCenter);
        var touchPos = this.node.convertToWorldSpace(event);
        
        localPos = cc.v2(localPos.x - this.node.width / 2, localPos.y - this.node.height / 2);
        this.distanceToCircle = this.distance(this.circleCenter, localPos);
        if(this.distanceToCircle >= this.touchStop && this.touchValid){
            this.updateStick(localPos);
            
        }
        else {
            this.touchValid = false;
            this.stopStick();
        }

        event.stopPropagation();
    },

    onTouchEnd: function(event) {
        this.stopStick();
        this.touchValid = true;

        event.stopPropagation();
    },

    onTouchCancel: function(event) {
        this.stopStick();
        this.touchValid = true;

        event.stopPropagation();
    },

    updateStick: function(pos) {
        if (pos == cc.Vec2.ZERO){
            this.stopStick();
        }
        else {
           // cc.log(pos);
            var angle1 = pos.signAngle(cc.v2(1, 0));
            var localPos = cc.v2(Math.cos(angle1) * 50, Math.sin(angle1) * 50);
            this.stick.setPosition(localPos);
            
           //var angle = Math.atan( pos.x / pos.y);
            var angle = Math.atan2(localPos.y - this.circleCenter.y, localPos.x - this.circleCenter.x);//计算弧度
            var angleHero = angle *  (180 / Math.PI); //转换为度

            //this.plane.avatar.rotation = -angleHero;
            //this.plane.heroMove(angle);
             var event = new cc.Event.EventCustom('joystick_move', true);
             event.setUserData({angle: angle});
             this.node.dispatchEvent(event);
        }
    },

    stopStick: function(){
        this.stick.opacity = 255;
        this.stick.setPosition(cc.Vec2.ZERO);

        var event = new cc.Event.EventCustom('joystick_stop', true);
        this.node.dispatchEvent(event);
    },
    distance: function(pos1, pos2){
        return Math.sqrt(Math.pow(pos1.x - pos2.x, 2) +  Math.pow(pos1.y - pos2.y, 2));
    }
});
